% Mengzi Zhang
% 16 Nov 2011
% CIS 520 Project

% Unsupervised - Kmeans clustering
%   Uses Spider library
%   Assume data .mat file is loaded before calling this script

% Can't really check errors, since that notion of Y lbls doesn't exist in
% unsupervised training. Cluster 1 could be star 4, cluster 4 could be star 2,
% there's no way to compare the numbers to check error.


%% CV training / test sets

XcvTrain = make_sparse(train(bsxfun(@gt, [train().category], 6)));
YcvTrain = double([train(bsxfun(@gt, [train().category], 6)).rating])';

XcvTest = make_sparse(train(bsxfun(@lt, [train().category], 7)));
YcvTest = double([train(bsxfun(@lt, [train().category], 7)).rating])';


%% Train

% Set up kmeans obj
kmeans_obj = kmeans;
kmeans_obj.k = 4;
% This doesn't work, for some reason.
%kmeans_obj.child = distance ('euclid');

% Train
% No output Y data for clustering. Create with one var
data_train = data (XcvTrain);
% Use feval, because supplied data has a var named train. Using train() will
%   point to that var, instead of the Spider fn.
[predictions_tr model] = feval ('train', kmeans_obj, data_train);


%% Plot

% See the predictions
predictions_tr.X
% cluster centers
%model.mu
% cluster assignment of each training sample
%model.y

% idx for examples with predicted label 1
idx = find (predictions_tr.X == 1);
clf;
hold on;
plot (data_train.X(idx, 1), data_train.X(idx, 2), 'r.');

% idx for examples with predicted label 2
idx = find (predictions_tr.X == 2);
plot (data_train.X(idx, 1), data_train.X(idx, 2), 'b.');

idx = find (predictions_tr.X == 3);
plot (data_train.X(idx, 1), data_train.X(idx, 2), 'g.');


%% Test

% Test
data_test = data (XcvTest);
predictions_ts = feval ('test', model, data_test);

% See the predictions
predictions_ts.X


